﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MxWeiXinPF.Web.UI;
using System.Web.UI.HtmlControls;
using MxWeiXinPF.Common;
using System.Text;

namespace MxWeiXinPF.Web.admin.users
{
    public partial class indexcatepic_list : ManagePage
    {
        protected MxWeiXinPF.Model.manager admin_info;
        protected string keywords = string.Empty;
        protected int page;
        protected int pageSize = 10;
        protected int shopid = 0;
        protected int totalCount;
        protected int wid = 0;

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            base.ChkAdminLevel("indexcatepic_list", MXEnums.ActionEnum.Delete.ToString());
            int num = 0;
            int num2 = 0;
            MxWeiXinPF.BLL.indexcatepic indexcatepic = new MxWeiXinPF.BLL.indexcatepic();
            Repeater repeater = this.rptList2;
            for (int i = 0; i < repeater.Items.Count; i++)
            {
                int id = Convert.ToInt32(((HiddenField)repeater.Items[i].FindControl("hidId")).Value);
                CheckBox box = (CheckBox)repeater.Items[i].FindControl("chkId");
                if (box.Checked)
                {
                    if (indexcatepic.Delete(id))
                    {
                        num++;
                    }
                    else
                    {
                        num2++;
                    }
                }
            }
            base.AddAdminLog(MXEnums.ActionEnum.Edit.ToString(), string.Concat(new object[] { "删除首页幻灯片成功", num, "条，失败", num2, "条" }));
            base.JscriptMsg(string.Concat(new object[] { "删除成功", num, "条，失败", num2, "条！" }), Utils.CombUrlTxt("indexcatepic_list.aspx", "shopid={0}&keywords={2}", new string[] { this.shopid.ToString(), this.keywords }), "Success");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            base.ChkAdminLevel("indexcatepic_list", MXEnums.ActionEnum.Edit.ToString());
            MxWeiXinPF.BLL.indexcatepic indexcatepic = new MxWeiXinPF.BLL.indexcatepic();
            Repeater repeater = new Repeater();
            repeater = this.rptList2;
            for (int i = 0; i < repeater.Items.Count; i++)
            {
                int num3;
                int id = Convert.ToInt32(((HiddenField)repeater.Items[i].FindControl("hidId")).Value);
                if (!int.TryParse(((TextBox)repeater.Items[i].FindControl("txtSortId")).Text.Trim(), out num3))
                {
                    num3 = 0x63;
                }
                indexcatepic.UpdateField(id, "sort_id=" + num3.ToString());
            }
            base.AddAdminLog(MXEnums.ActionEnum.Edit.ToString(), "保存首页幻灯片排序成功");
            base.JscriptMsg("保存排序成功啦！", Utils.CombUrlTxt("indexcatepic_list.aspx", "shopid={0}&keywords={1}", new string[] { this.shopid.ToString(), this.keywords }), "Success");
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            base.Response.Redirect(Utils.CombUrlTxt("indexcatepic_list.aspx", "shopid={0}&keywords={1}", new string[] { this.shopid.ToString(), this.txtKeywords.Text }));
        }

        protected string CombSqlTxt(int shopid, string _keywords)
        {
            StringBuilder builder = new StringBuilder();
            _keywords = _keywords.Replace("'", "");
            if (!string.IsNullOrEmpty(_keywords))
            {
                builder.Append(" and title like '%" + _keywords + "%'");
            }
            if (shopid == 0)
            {
                this.admin_info = base.GetAdminInfo();
                shopid = this.admin_info.id;
                builder.Append(" and shopid=" + this.shopid);
            }
            else
            {
                builder.Append(" and shopid=" + this.shopid);
            }
            return builder.ToString();
        }

        protected string GetName(string categoryid)
        {
            MxWeiXinPF.BLL.wx_shop_category _category = new MxWeiXinPF.BLL.wx_shop_category();
            return _category.GetName(categoryid);
        }

        private int GetPageSize(int _default_size)
        {
            int num;
            if (int.TryParse(Utils.GetCookie("indexcatepic_page_size"), out num) && (num > 0))
            {
                return num;
            }
            return _default_size;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (!this.Page.IsPostBack)
                {
                    base.ChkAdminLevel("indexcatepic_list", MXEnums.ActionEnum.View.ToString());
                    this.keywords = MXRequest.GetQueryString("keywords");
                    this.shopid = MXRequest.GetQueryInt("shopid");
                    if (this.wid == 0)
                    {
                        MxWeiXinPF.Model.manager weiXinCode = base.GetAdminInfo();
                        this.wid = weiXinCode.id;
                    }
                    if (this.shopid == 0)
                    {
                        this.admin_info = base.GetAdminInfo();
                        this.shopid = this.admin_info.id;
                    }
                    this.RptBind("id>0" + this.CombSqlTxt(this.shopid, this.keywords), "sort_id asc,add_time desc,id desc");
                }
            }
            catch (Exception ex)
            {
                
            }

        }

        private void RptBind(string _strWhere, string _orderby)
        {
            MxWeiXinPF.Model.manager weiXinCode = base.GetAdminInfo();
            this.page = MXRequest.GetQueryInt("page", 1);
            this.txtKeywords.Text = this.keywords;
            this.rptList2.DataSource = new MxWeiXinPF.BLL.indexcatepic().GetWCodeList(weiXinCode.id, this.pageSize, this.page, _strWhere, _orderby, out this.totalCount);
            this.rptList2.DataBind();
            this.txtPageNum.Text = this.pageSize.ToString();
            string linkUrl = Utils.CombUrlTxt("indexcatepic_list.aspx", "shopid={0}&keywords={0}&page={1}", new string[] { this.shopid.ToString(), this.keywords, "__id__" });
            this.PageContent.InnerHtml = Utils.OutPageList(this.pageSize, this.page, this.totalCount, linkUrl, 8);
        }

        protected void txtPageNum_TextChanged(object sender, EventArgs e)
        {
            int num;
            if (int.TryParse(this.txtPageNum.Text.Trim(), out num) && (num > 0))
            {
                Utils.WriteCookie("indexcatepic_page_size", num.ToString(), 0xa8c0);
            }
            base.Response.Redirect(Utils.CombUrlTxt("indexcatepic_list.aspx", "shopid={0}&keywords={1}", new string[] { this.shopid.ToString(), this.keywords }));
        }
    }
}